#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->actionEnglish,
            SIGNAL(triggered(bool)),
            this,
            SLOT(on_actionEnglish_triggered(void)));
    connect(ui->actionGerman,
            SIGNAL(triggered(bool)),
            this,
            SLOT(on_actionGerman_triggered(void)));
    connect(ui->actionTurkish,
            SIGNAL(triggered(bool)),
            this,
            SLOT(on_actionTurkish_triggered(void)));
    connect(ui->actionChinese,
            SIGNAL(triggered(bool)),
            this,
            SLOT(on_actionChinese_triggered(void)));
    loadSettings();

    turkishTranslator = new QTranslator(this);
    turkishTranslator->load(":/translations/Hello_Qt_OpenCV_tr.qm");

    germanTranslator = new QTranslator(this);
    germanTranslator->load(":/translations/Hello_Qt_OpenCV_de.qm");

    englishTranslator = new QTranslator(this);
    englishTranslator->load(":/translations/Hello_Qt_OpenCV_en.qm");
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_inputPushButton_pressed()
{
    QString fineName = QFileDialog::getOpenFileName(this,
                                                    /*"Open Input Image"*/tr("打开输入图片"),
                                                    QDir::currentPath(),
                                                    tr("Images (*.jpg *.png *.bmp)"));

    if (QFile::exists(fineName))
    {
        ui->inputLineEdit->setText(fineName);
    }
}

void MainWindow::on_outputPushButton_pressed()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                                    /* "Select Output Image" */tr("选择输出图片"),
                                                    QDir::currentPath(),
                                                    tr("*.jpg;;*.png;;*.bmp"));

    if (!fileName.isEmpty())
    {
        ui->outputLineEdit->setText(fileName);
        using  namespace cv;
        Mat inImg, outImg;

        inImg = imread(ui->inputLineEdit->text().toStdString());

        if (ui->medianBlurRadioButton->isChecked())
        {
            cv::medianBlur(inImg, outImg, 5);
        }
        else if(ui->gaussianBlurRadioButton->isChecked())
        {
            cv::GaussianBlur(inImg, outImg, Size(5, 5), 1.25);
        }

        imwrite(fileName.toStdString(), outImg);

        if (ui->displayImageCheckBox->isChecked())
        {
            //imshow("Output Image", outImg);
            imshow(tr("输出图片").toStdString(), outImg);
        }

    }

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int ret = QMessageBox::warning(this, tr("结束"), tr("确定退出程序吗"), QMessageBox::Yes, QMessageBox::No);

    if(ret == QMessageBox::Yes)
    {
        saveSettings();
        event->accept();
    }
    else
    {
        event->ignore();
    }
}
void MainWindow::changeEvent(QEvent *event)
{
    if(event->type() == QEvent::LanguageChange)
    {
        ui->retranslateUi(this);
    }
    else
    {
        QMainWindow::changeEvent(event);
    }
}

void MainWindow::loadSettings(void)
{

    QSettings settings("Packt", "Hello_Qt_OpenCV", this);

    ui->inputLineEdit->setText(settings.value("inputLineEdit", "").toString());
    ui->outputLineEdit->setText(settings.value("outputLineEdit", "").toString());
    ui->medianBlurRadioButton->setChecked(settings.value("medianBlurRadioButton", true).toBool());
    ui->gaussianBlurRadioButton->setChecked(settings.value("gaussianBlurRadioButton", false).toBool());
    ui->displayImageCheckBox->setChecked(settings.value("displayImageCheckBox", false).toBool());
}

void MainWindow::saveSettings(void)
{
    QSettings settings("Packt", "Hello_Qt_OpenCV", this);

    settings.setValue("inputLineEdit", ui->inputLineEdit->text());
    settings.setValue("outputLineEdit", ui->outputLineEdit->text());
    settings.setValue("medianBlurRadioButton", ui->medianBlurRadioButton->isChecked());
    settings.setValue("gaussianBlurRadioButton", ui->gaussianBlurRadioButton->isChecked());
    settings.setValue("displayImageCheckBox", ui->displayImageCheckBox->isChecked());
}

void MainWindow::on_actionChinese_triggered(void)
{
    qApp->removeTranslator(turkishTranslator);
    qApp->removeTranslator(germanTranslator);
    qApp->removeTranslator(englishTranslator);
}

void MainWindow::on_actionGerman_triggered(void)
{
    qApp->installTranslator(germanTranslator);
}

void MainWindow::on_actionTurkish_triggered(void)
{
    qApp->installTranslator(turkishTranslator);
}
void MainWindow::on_actionEnglish_triggered(void)
{
    qApp->installTranslator(englishTranslator);
}
